package com.hxx.common.core.wcontour;

import com.alibaba.fastjson.JSON;
import com.hxx.common.core.wcontour.util.FeatureSurfaceUtil;
import com.hxx.common.core.wcontour.util.FeatureUtil;
import com.hxx.common.core.wcontour.util.GeoImageUtil;
import com.hxx.common.core.wcontour.base.TrainData;
import com.hxx.common.core.wcontour.enums.RadarColorEnum;
import com.hxx.common.core.wcontour.read.RadarBinUtil;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.map.Layer;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class TestRadar {

    public static void main(String[] args) throws IOException, SchemaException {
        String radarBinPath = "/Users/***/工作/绘天/气象项目/Radar-2022-09-23-14时左右/Z_OTHE_RADAMCR_20220923062400.bin";

        String boundryFile = "/Users/***/test/border_line/440000_borderline.shp";
        // 数据分层级别(数据间隙)
        double[] dataInterval = RadarColorEnum.getValueArray();
        FeatureSurfaceUtil equiSurface = new FeatureSurfaceUtil();
        // 读取雷达bin文件
        TrainData trainData = RadarBinUtil.read(new File(radarBinPath));
        int[] size = trainData.getSize();
        System.out.println(String.format("开始进行等值面分析，经纬度个数和温度值数量为：%s, %s", size[0], size[1]));

        long start = System.currentTimeMillis();

        FeatureCollection featureCollection = equiSurface.equiSurface(trainData, dataInterval);
        // 广东省裁切
//        featureCollection = FeatureClipUtil.clipFeatureCollection(featureCollection, boundryFile);
        

        System.out.println("等值面分析完成，总花时：" + (System.currentTimeMillis() - start));

        GeoImageUtil geoImageUtil = new GeoImageUtil();
        // 添加样式 透明度，颜色级别
        start = System.currentTimeMillis();
        float opacity = 1f;
        Map<Double, String> levelProps = RadarColorEnum.getValueColorMap();
        Layer layer = FeatureUtil.addShapeLayer(featureCollection, levelProps, opacity);
        System.out.println("添加样式，总花时：" + (System.currentTimeMillis() - start));

        // 输出图片
        String imgPath = "/Users/***/test/radar/裁切图片_Y_X.png";

        // 获取shp边界，生成格网点
        double minX = 106.55;
        double minY = 17.695;
        double maxX = 119.75;
        double maxY = 28.005;
        double[] bbox = {minX, minY, maxX, maxY};
        int[] _xy = {1000, 800};
        GeoImageUtil.layerToPng(layer, bbox, _xy, imgPath);
        System.out.println("输出图片完成");

        // 输出geoJson
        String geojson = "/Users/***/test/radar/geojson.json";
        JSON geoJson = FeatureUtil.featureToGeoJson(featureCollection);
        Files.writeString(Paths.get(geojson), geoJson.toJSONString());
        System.out.println("输出geoJson完成");
    }
}
